/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interfaz;

import Excepciones.BuscarDatosEmergenciaException;
import Excepciones.BuscarDatosOperadorException;
import Excepciones.BuscarDatosAdministradorException;
import Excepciones.BuscarDatosResponsableException;
import Excepciones.BuscarDatosSupervisorException;
import Excepciones.GuardarDatosResponsableException;
import Negocio.GestorOperador;
import Negocio.GestorAdministrador;
import Negocio.GestorRepresentanteEntidad;
import Negocio.GestorSupervisor;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author vanegas
 */

public class Login extends javax.swing.JFrame {

    /**
     * Creates new form Login_
     */
    GestorOperador gestorOperador=new GestorOperador();
    GestorAdministrador gestoradministrador =new GestorAdministrador();
    GestorSupervisor gestorsupervisor=new GestorSupervisor();
    GestorRepresentanteEntidad gestorrepresentante = new GestorRepresentanteEntidad();
    public static String fecha;
    
    
    
    public Login() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usuariologtxt = new javax.swing.JTextField();
        logcontraseñatxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        cargobox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Usuario: ");

        jLabel3.setText("Contraseña:");

        jToggleButton1.setText("Entrar");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("LOGIN");

        cargobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Operador", "Representante", "Supervisor" }));
        cargobox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargoboxActionPerformed(evt);
            }
        });

        jLabel4.setText("Cargo:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(139, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(125, 125, 125))
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cargobox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(usuariologtxt)
                    .addComponent(logcontraseñatxt)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(usuariologtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(logcontraseñatxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cargobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addComponent(jToggleButton1)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        
        String seleccionado=(String)cargobox.getSelectedItem();
        if(seleccionado.equals("Administrador")){
            ArrayList datos=new ArrayList();
            try {
                datos=gestoradministrador.BuscarUsuario(usuariologtxt.getText(), logcontraseñatxt.getText());
                if(datos.get(5).toString().equals(logcontraseñatxt.getText().toString())){
                Administrador ad= new Administrador();
                ad.setVisible(true);
                this.setVisible(false);
                Administrador.jLabel20.setText((String) datos.get(4));
                }
                else{
                   JOptionPane.showMessageDialog(null,"Contraseña mal ingresada"); 
                }
            }catch (BuscarDatosAdministradorException ex) {
                JOptionPane.showMessageDialog(null,"Error, Verifique que el usuario exista");
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else if(seleccionado.equals("Operador")){
            ArrayList datos=new ArrayList();
            try {
                datos=gestorOperador.BuscarUsuario(usuariologtxt.getText(), logcontraseñatxt.getText());
                if(datos.get(5).toString().equals(logcontraseñatxt.getText().toString())){
                    Operador ce= new Operador();
                    ce.setVisible(true);
                    Calendar Cal= Calendar.getInstance();
                    fecha= Cal.get(Cal.DATE)+"/"+(Cal.get(Cal.MONTH)+1)+"/"+Cal.get(Cal.YEAR);
                    Operador.fechalbl.setText(fecha);
                    Operador.usuariolbl.setText((String) datos.get(4)); 
                    this.setVisible(false);
                }
                 else{
                   JOptionPane.showMessageDialog(null,"Contraseña mal ingresada"); 
                }
             }catch (BuscarDatosOperadorException ex) {
                JOptionPane.showMessageDialog(null,"Error, Verifique que el usuario exista");
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }    
            
        }
        else if(seleccionado.equals("Representante")){
            ArrayList datos=new ArrayList();
            try {            
                datos=gestorrepresentante.BuscarUsuario(usuariologtxt.getText(), logcontraseñatxt.getText());
                if(datos.get(5).toString().equals(logcontraseñatxt.getText().toString())){
                    Representante re= new Representante();
                    re.setVisible(true);
                    Representante.jLabel3.setText((String) datos.get(0));
                    Representante.jLabel7.setText("");
                    this.setVisible(false);
                }
                 else{
                   JOptionPane.showMessageDialog(null,"Contraseña mal ingresada"); 
                }
            } catch (BuscarDatosResponsableException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (GuardarDatosResponsableException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
                
           
        }
        else if(seleccionado.equals("Supervisor")){
            ArrayList datos=new ArrayList();
            try {
                datos=gestorsupervisor.BuscarUsuario(usuariologtxt.getText(), logcontraseñatxt.getText());
                Supervisor su= new Supervisor();
                su.setVisible(true);
                Supervisor.jLabel3.setText((String) datos.get(0));
                this.setVisible(false);
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (BuscarDatosSupervisorException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
        
       

    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void cargoboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargoboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cargoboxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cargobox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTextField logcontraseñatxt;
    private javax.swing.JTextField usuariologtxt;
    // End of variables declaration//GEN-END:variables
}
